package com.ss.android.common.app;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.accessibility.AccessibilityManagerCompat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityManager;
import android.webkit.WebView;
import android.widget.ImageView;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.LoadUrlUtils;
import com.ss.android.common.util.ToolUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashSet;

public final class WebViewTweaker {
    public static final String BLANK_URL = "about:blank";

    private static final boolean sIsBuggyMIUI = Build.VERSION.SDK_INT >= 16 && ToolUtils.isMiui();
    private static int b = -1;
    private static int sTweakWebviewDrawingCache = -1;
    private static Handler d = null;
    private static HashSet sDeviceSet;
    private static HashSet sDeviceSetZte;
    private static AccessibilityManagerCompat.AccessibilityStateChangeListenerCompat sAccessibilityStateChangeListener;

    static {
        HashSet hashSet = new HashSet();
        hashSet.add("HUAWEI C8812");
        hashSet.add("HUAWEI C8812E");
        hashSet.add("HUAWEI C8825D");
        hashSet.add("HUAWEI U8825D");
        hashSet.add("HUAWEI C8950D");
        hashSet.add("HUAWEI U8950D");
        WebViewTweaker.sDeviceSet = hashSet;
        HashSet<String> hashSet2 = new HashSet();
        hashSet2.add("ZTE V955");
        hashSet2.add("ZTE N881E");
        hashSet2.add("ZTE N881F");
        hashSet2.add("ZTE N880G");
        hashSet2.add("ZTE N880F");
        hashSet2.add("ZTE V889F");
        WebViewTweaker.sDeviceSetZte = hashSet2;
    }

    public static void clearWebviewOnDestroy(WebView webView) {
        if (webView != null) {
            webView.setWebChromeClient(null);
            webView.setWebViewClient(null);
            ViewParent parent = webView.getParent();
            if (!(parent instanceof ViewGroup)) {
                return;
            }

            ((ViewGroup) parent).removeView(((View) webView));
            try {
                webView.destroy();
            } catch (Throwable throwable) {
            }
        }
    }

    public static void tweakPauseIfFinishing(Context context, WebView webView) {
        if (context != null && (webView != null && ((context instanceof Activity)) && (((Activity) context).isFinishing()))) {
            try {
                LoadUrlUtils.loadUrl(webView, BLANK_URL);
                if (WebViewTweaker.sTweakWebviewDrawingCache > 0) {
                    View rootView = webView.getRootView();
                    if (!(rootView instanceof ViewGroup)) {
                    } else {
                        View child = ((ViewGroup) rootView).getChildAt(0);
                        child.setDrawingCacheEnabled(true);
                        Bitmap bitmap = Bitmap.createBitmap(child.getDrawingCache());
                        child.setDrawingCacheEnabled(false);
                        ImageView imageView = new ImageView(context);
                        imageView.setImageBitmap(bitmap);
                        imageView.setVisibility(View.VISIBLE);
                        ((ViewGroup) rootView).addView(imageView, new ViewGroup.LayoutParams(-1, -1));
                    }
                }
            } catch (Exception e) {
            }

            if (Logger.debug()) {
                Logger.v("WebViewTweaker", "tweak webview pause when finishing");
            }
        }
    }

    public static void a() {
    }

    public static void a(int arg0) {
        WebViewTweaker.b = arg0;
    }

    public static void tryTweakWebCoreHandler(boolean arg6, boolean arg7, boolean arg8) {
        int v1 = 0;
        int v2 = 1;
        try {
            if (WebViewTweaker.d != null) {
                return;
            }

            if (Build.VERSION.SDK_INT == 15) {
                if (!arg8 && (!arg6 || !WebViewTweaker.sDeviceSet.contains(Build.MODEL))) {
                    if (!arg7) {
//                        goto label_20;
                    } else if (!WebViewTweaker.sDeviceSetZte.contains(Build.MODEL)) {
//                        goto label_20;
                    } else {
                        v1 = 1;
                    }
                } else {
                    v1 = 1;
                }
            }

//            label_20:
            if (WebViewTweaker.b <= 0) {
                if (WebViewTweaker.b < 0) {
                    if (!WebViewTweaker.sIsBuggyMIUI) {
                        v2 = v1;
                    }
                } else {
                    v2 = v1;
                }
            }
            if (v2 == 0) {
                if (WebViewTweaker.d != null) {
                    return;
                }
                WebViewTweaker.d = new Handler();
                return;
            }

            if (WebViewTweaker.d != null) {
                return;
            }
            Field v3 = Class.forName("android.webkit.WebViewCore").getDeclaredField("sWebCoreHandler");
            v3.setAccessible(true);
            Object v2_1 = v3.get(null);
            if (v2_1 == null) {
                return;
            }

            if (!(v2_1 instanceof Handler)) {
                WebViewTweaker.d = new Handler();
                return;
            }

            Method v1_2 = Handler.class.getDeclaredMethod("getIMessenger");
            v1_2.setAccessible(true);
            Object v4 = v1_2.invoke(v2_1);
            WebViewTweaker.d = new WebCoreProxyHandler((Handler) v2_1);
            if (v4 != null) {
                Field v1_3 = Handler.class.getDeclaredField("mMessenger");
                v1_3.setAccessible(true);
                v1_3.set(WebViewTweaker.d, v4);
            }

            v3.set(null, WebViewTweaker.d);
            Logger.w("WebViewTweaker", "sWebCoreHandler: " + v2_1);
            if (WebViewTweaker.d != null) {
                return;
            }
            WebViewTweaker.d = new Handler();
            return;
        } catch (Throwable v1_1) {
            Logger.w("WebViewTweaker", "tweak WebCoreHandler exception: " + v1_1);
        }
    }

    static void setAccessibilityState(AccessibilityManager accessibilityManager) {
        try {
            Method method = AccessibilityManager.class.getDeclaredMethod("setAccessibilityState", Boolean.TYPE);
            if (method != null) {
                method.setAccessible(true);
                method.invoke(accessibilityManager, false);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public static void setTweakWebviewDrawingCache(int tweakWebviewDrawingCache) {
        WebViewTweaker.sTweakWebviewDrawingCache = tweakWebviewDrawingCache;
    }

    public static void tryDisableAccessibility() {
        if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 18 && WebViewTweaker.sAccessibilityStateChangeListener == null) {
            final AccessibilityManager manager = (AccessibilityManager) AbsApplication.getApplication().getSystemService(Context.ACCESSIBILITY_SERVICE);
            if (manager != null) {
                sAccessibilityStateChangeListener = new AccessibilityManagerCompat.AccessibilityStateChangeListenerCompat() {
                    @Override
                    public final void onAccessibilityStateChanged(boolean enabled) {
                        if (Logger.debug()) {
                            Logger.d("Accessibility", "WebViewTweaker.onAccessibilityStateChanged:" + enabled);
                        }
                        if (enabled) {
                            WebViewTweaker.setAccessibilityState(manager);
                        }
                    }
                };
                AccessibilityManagerCompat.addAccessibilityStateChangeListener(manager, sAccessibilityStateChangeListener);
                if (manager.isEnabled()) {
                    WebViewTweaker.setAccessibilityState(manager);
                }
            }
        }
    }

    static final class WebCoreProxyHandler extends Handler {
        private Handler handler;

        public WebCoreProxyHandler(Handler handler) {
            super(handler.getLooper());
            this.handler = handler;
        }

        public final void dispatchMessage(Message message) {
            try {
                super.dispatchMessage(message);
            } catch (Throwable throwable) {
                Log.w("WebCoreProxyHandler", "dispatchMessage exception: " + throwable);
            }
        }

        public final void handleMessage(Message message) {
            try {
                this.handler.handleMessage(message);
            } catch (Throwable throwable) {
                Log.w("WebCoreProxyHandler", "handleMessage exception: " + throwable);
            }
        }
    }
}

